package com.baobao.web;

import com.baobao.dao.SchoolDAO;

import com.baobao.domain.School;

import com.baobao.service.SchoolService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for School entities
 * 
 */

@Controller("SchoolController")
public class SchoolController {

	/**
	 * DAO injected by Spring that manages School entities
	 * 
	 */
	@Autowired
	private SchoolDAO schoolDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for School entities
	 * 
	 */
	@Autowired
	private SchoolService schoolService;

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Select an existing School entity
	 * 
	 */
	@RequestMapping("/selectSchool")
	public ModelAndView selectSchool(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("school", schoolDAO.findSchoolByPrimaryKey(idKey));
		mav.setViewName("school/viewSchool.jsp");

		return mav;
	}

	/**
	 * Entry point to show all School entities
	 * 
	 */
	public String indexSchool() {
		return "redirect:/indexSchool";
	}

	/**
	 * Edit an existing School entity
	 * 
	 */
	@RequestMapping("/editSchool")
	public ModelAndView editSchool(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("school", schoolDAO.findSchoolByPrimaryKey(idKey));
		mav.setViewName("school/editSchool.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/schoolController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Show all School entities
	 * 
	 */
	@RequestMapping("/indexSchool")
	public ModelAndView listSchools() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("schools", schoolService.loadSchools());

		mav.setViewName("school/listSchools.jsp");

		return mav;
	}

	/**
	 * Create a new School entity
	 * 
	 */
	@RequestMapping("/newSchool")
	public ModelAndView newSchool() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("school", new School());
		mav.addObject("newFlag", true);
		mav.setViewName("school/editSchool.jsp");

		return mav;
	}

	/**
	 * Select the School entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteSchool")
	public ModelAndView confirmDeleteSchool(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("school", schoolDAO.findSchoolByPrimaryKey(idKey));
		mav.setViewName("school/deleteSchool.jsp");

		return mav;
	}

	/**
	 * Save an existing School entity
	 * 
	 */
	@RequestMapping("/saveSchool")
	public String saveSchool(@ModelAttribute School school) {
		schoolService.saveSchool(school);
		return "forward:/indexSchool";
	}

	/**
	 * Delete an existing School entity
	 * 
	 */
	@RequestMapping("/deleteSchool")
	public String deleteSchool(@RequestParam String idKey) {
		School school = schoolDAO.findSchoolByPrimaryKey(idKey);
		schoolService.deleteSchool(school);
		return "forward:/indexSchool";
	}
}